{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>大纲<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#函数参数信息\" data-toc-modified-id=\"函数参数信息-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>函数参数信息</a></span><ul class=\"toc-item\"><li><span><a href=\"#通过函数对象属性获取\" data-toc-modified-id=\"通过函数对象属性获取-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>通过函数对象属性获取</a></span></li><li><span><a href=\"#通过inspect模块获取\" data-toc-modified-id=\"通过inspect模块获取-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>通过inspect模块获取</a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数参数信息\n",
    "## 通过函数对象属性获取\n",
    "    - `__defaults__`\n",
    "    - `__code__`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-24T07:06:17.000305Z",
     "start_time": "2018-05-24T07:06:16.995541Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(80,)\n",
      "2\n",
      "('text', 'max_len', 'kwargs', 'end', 'space_before', 'space_after')\n"
     ]
    }
   ],
   "source": [
    "def clip(text, max_len=80,**kwargs):\n",
    "    \"\"\"Return text clipped at the last space before or after max_len\n",
    "    \"\"\"\n",
    "    end = None\n",
    "    if len(text) > max_len:\n",
    "        space_before = text.rfind(' ', 0, max_len)\n",
    "        if space_before >= 0:\n",
    "            end = space_before\n",
    "        else:\n",
    "            space_after = text.rfind(' ', max_len)\n",
    "            if space_after >= 0:\n",
    "                end = space_after\n",
    "    if end is None:  # no spaces were found\n",
    "        end = len(text)\n",
    "    return text[:end].rstrip()\n",
    "\n",
    "# 包含参数的默认\n",
    "print(clip.__defaults__)\n",
    "\n",
    "# 包含参数的个数(不含变长参数)\n",
    "print(clip.__code__.co_argcount)\n",
    "\n",
    "# 前N个变量名,使参数名\n",
    "print(clip.__code__.co_varnames)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 通过inspect模块获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-24T07:07:43.024345Z",
     "start_time": "2018-05-24T07:07:43.020332Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Signature (text, max_len=80, **kwargs)>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from inspect import signature\n",
    "sig = signature(clip)\n",
    "sig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-24T07:07:59.226818Z",
     "start_time": "2018-05-24T07:07:59.220468Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'(text, max_len=80, **kwargs)'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(sig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-24T07:09:17.540930Z",
     "start_time": "2018-05-24T07:09:17.538060Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "POSITIONAL_OR_KEYWORD : text = <class 'inspect._empty'>\n",
      "POSITIONAL_OR_KEYWORD : max_len = 80\n",
      "VAR_KEYWORD : kwargs = <class 'inspect._empty'>\n"
     ]
    }
   ],
   "source": [
    "for name,param in sig.parameters.items():\n",
    "    print(param.kind,\":\",name,\"=\",param.default)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda all lib",
   "language": "python",
   "name": "base"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "大纲",
   "title_sidebar": "目录",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
